#! /bin/bash

gateway=$1
prefix=$2
nodeinfo=$3

function changeIP()
{
    file=$1
    gate=$2
    prefix=$3
    node=$4

    ip=${gate%.*}"."$node

    # 删除原来的IP信息
    sed -i "/^IPADDR/d" $file

    # 把IP信息写入
    echo "IPADDR=$ip" >> $file

    # 删除原有的GATEWAY
    sed -i "/^GATEWAY/d" $file

    # 写入GATEWAY
    echo "GATEWAY=$gate" >> $file

    # 删除原有的PREFIX
    sed -i "/^PREFIX/d" $file

    # 写入PREFIX
    echo "PREFIX=$prefix" >> $file

    # 重启网络
    systemctl restart network
}

changeIP /etc/sysconfig/network-scripts/ifcfg-enp0s3 $gateway $prefix $nodeinfo
